<template>
	<view>
		<cu-custom bgColor="bg-gradual-fff" isBack="true">
			<block slot="content">修改信息</block>
		</cu-custom>

		<view class="edit-container">
			<!-- 头像区域 -->
			<!-- <view class="avatar-section">
        <image class="avatar" :src="request.baseUrl_img + userinfo.avatar" mode="aspectFill"></image>
        <text class="avatar-text">点击修改头像</text>
      </view> -->

			<!-- 表单区域 -->
			<view class="form-section">
				<view class="form-item">
					<text class="form-label">用户名</text>
					<u-input v-model="editForm.username" placeholder="请输入用户名" border="bottom" clearable></u-input>
				</view>

				<view class="form-item">
					<text class="form-label">手机号</text>
					<u-input v-model="editForm.phone" placeholder="请输入手机号" type="number" border="bottom"
						clearable></u-input>
				</view>
			</view>

			<!-- 保存按钮 -->
			<view class="submit-btn">
				<u-button type="primary" @click="submitEdit" :loading="submitting" shape="circle">保存修改</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '@/common/request.js';
	import storage from '@/common/storage.js';

	export default {
		data() {
			return {
				id: 0,
				userinfo: {
					// avatar: '/static/images/avatar/avatar1.jpg',
					username: '',
					phone: ''
				},
				submitting: false,
				editForm: {
					username: '',
					phone: ''
				}
			}
		},
		methods: {
			// 获取用户信息
			getuserinfo() {
				let optsList = {
					url: `api/user/` + this.id,
					method: 'get'
				};
				let _this = this;
				request.httpRequest(optsList).then(res => {
					if (res.data.code == 200) {
						_this.userinfo = res.data.data;
						_this.editForm = {
							username: res.data.data.username,
							phone: res.data.data.phone
						};
					}
				}).catch(err => {
					console.error('获取用户信息失败:', err);
					uni.showToast({
						title: '获取用户信息失败',
						icon: 'none'
					});
				});
			},

			// 提交修改
			submitEdit() {
				if (!this.editForm.username) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					});
					return;
				}

				if (!/^1[3-9]\d{9}$/.test(this.editForm.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}

				this.submitting = true;

				let optsList = {
					url: 'api/user',
					method: 'put'
				};

				let om = {

					id: this.id,
					username: this.editForm.username,
					phone: this.editForm.phone

				}

				request.httpRequest(optsList, om).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '修改成功'
						});
						storage.setStorageSync('name', this.editForm.username);
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					} else {
						uni.showToast({
							title: res.data.msg || '修改失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('修改用户信息失败:', err);
					uni.showToast({
						title: '修改失败',
						icon: 'none'
					});
				}).finally(() => {
					this.submitting = false;
				});
			}
		},
		mounted() {
			const id = uni.getStorageSync('mid');
			this.id = id;
			this.getuserinfo();
		}
	}
</script>

<style lang="scss" scoped>
	.edit-container {
		padding: 40rpx;
	}

	.avatar-section {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 60rpx;

		.avatar {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			border: 4rpx solid #fff;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
		}

		.avatar-text {
			margin-top: 20rpx;
			font-size: 26rpx;
			color: #666;
		}
	}

	.form-section {
		background: #fff;
		border-radius: 16rpx;
		padding: 0 30rpx;
		margin-bottom: 40rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
	}

	.form-item {
		padding: 30rpx 0;
		border-bottom: 1rpx solid #f5f5f5;

		&:last-child {
			border-bottom: none;
		}
	}

	.form-label {
		display: block;
		font-size: 28rpx;
		color: #333;
		margin-bottom: 20rpx;
		font-weight: bold;
	}

	.submit-btn {
		margin-top: 60rpx;
		padding: 0 30rpx;
	}

	/* 调整uView输入框样式 */
	/deep/ .u-input {
		padding-left: 0 !important;
	}

	/deep/ .u-input__input {
		font-size: 28rpx !important;
	}
</style>